package oldtest;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Vector;

import boardgame.GameState;
import boardgame.Move;
import boardgame.Piece;

public class AIGameState extends TicTacToeTest implements GameState {
		
	private TicTacToeTest currentState = new TicTacToeTest();
	
	public AIGameState(){
		
	}
	
	public AIGameState(TicTacToeTest cState){
		currentState = cState;
	}
	
	public void copyState(TicTacToeTest state){
		this.currentState.copy(state);
	}
	
	public GameState applyMove(final Move move) {
		TicTac temp = (TicTac)move;
		try {
			placePiece(temp);
			//this is messed, should be currentState.placePiece, but that doesn't work.
		} catch (Exception e){}		
		return this;
	}

	public int evaluate() {
		boolean[][][] occupied = currentState.occupiedCells();
		int value = 0;
		int o = currentPlayer%2;
		int c = currentPlayer-1;		
		for (int i = 0; i < 3; i++){			
			if(occupied[c][0][i]||occupied[c][1][i]||occupied[c][2][i]) {
				if (!occupied[o][0][i]&&!occupied[o][1][i]&&!occupied[o][2][i])
					value += 200;		
				if (occupied[c][0][i]&&occupied[c][1][i]&&occupied[c][2][i])
					return Integer.MAX_VALUE;
			} else {
				if (occupied[o][0][i]||occupied[o][1][i]||occupied[o][2][i])
					value -= 200;
				if (occupied[o][0][i]&&occupied[o][1][i]&&occupied[o][2][i])
					return -Integer.MAX_VALUE;
			}			
			if (occupied[c][i][0]||occupied[c][i][1]||occupied[c][i][2]){
				if (!occupied[o][i][0]&&!occupied[o][i][1]&&!occupied[o][i][2])
					value += 200;				
				if (occupied[c][i][0]&&occupied[c][i][1]&&occupied[c][i][2])
					return Integer.MAX_VALUE;
			} else {
				if(occupied[o][i][0]||occupied[o][i][1]||occupied[o][i][2])
					value -= 200;
				if(occupied[o][i][0]&&occupied[o][i][1]&&occupied[o][i][2])
					return -Integer.MAX_VALUE;
			}
		}
		if (occupied[c][0][0]||occupied[c][1][1]||occupied[c][2][2]){
			if (!occupied[o][0][0]&&!occupied[c][1][1]&&!occupied[c][2][2])
				value += 200;
			if (occupied[c][0][0]||occupied[c][1][1]||occupied[c][2][2])
				return Integer.MAX_VALUE;
		} else {
			if (occupied[o][0][0]||occupied[c][1][1]||occupied[c][2][2])
				value -= 200;
			if (occupied[o][0][0]&&occupied[c][1][1]&&occupied[c][2][2])
				return -Integer.MAX_VALUE;
		}			
		if (occupied[c][0][2]||occupied[c][1][1]||occupied[c][2][0]){
			if (!occupied[o][0][2]&&!occupied[c][1][1]&&!occupied[c][2][0])
				value += 200;
			if (occupied[c][0][2]||occupied[c][1][1]||occupied[c][2][0])
				return Integer.MAX_VALUE;
		} else {
			if (occupied[o][0][2]||occupied[c][1][1]||occupied[c][2][0])
				value -= 200;
			if (occupied[o][0][2]&&occupied[c][1][1]&&occupied[c][2][0])
				return -Integer.MAX_VALUE;
		}
		return value;
	}
	
	public Vector<Move> generateMoves() {
		boolean[][][] occupied = currentState.occupiedCells();
		Vector<Move> moves = new Vector<Move>();
		if (gameEnd())
			return moves;
		for (int i = 0; i < 3; i++){
			for (int j = 0; j < 3; j++){
				try {
					if (!occupied[0][i][j]&&!occupied[1][i][j])
						moves.add(new TicTac(i, j, currentState.getCurrentPlayer()));
				} catch (Exception e){}
			}
		}		
		return moves;
	}

	public GameState simulateMove(final Move move) {
		AIGameState newState = new AIGameState(currentState);
		//newState.copyState(currentState);
		return newState.applyMove(move);
	}
}
